//底部聊天组件
import 'package:color_dart/RgbaColor.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/provider/comment_model.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';
import 'package:provider/provider.dart';

class ChatInput extends StatefulWidget {
  final id;
  final type;
  final Function cancelInputView;
  const ChatInput({Key key, this.id, this.type, this.cancelInputView})
      : super(key: key);

  @override
  _ChatInputState createState() => _ChatInputState();
}

final TextEditingController _textController = TextEditingController(); //控制器
FocusNode _focusNode = FocusNode(); //键盘激活控制器

class _ChatInputState extends State<ChatInput> {
  bool isReplay = false;

  @override
  Widget build(BuildContext context) {
    CommentModelProvider commentProvider =
        Provider.of<CommentModelProvider>(context);
    return Container(
      height: 60.0,
      padding: EdgeInsets.only(left: 20, right: 10),
      child: Row(
        children: <Widget>[
          Expanded(
            child: TextField(
              maxLines: null,
              controller: _textController,
              focusNode: _focusNode,
              onSubmitted: _postComment,
              keyboardType: TextInputType.multiline,
              textInputAction: TextInputAction.send,
              decoration: InputDecoration(
                fillColor: rgba(249, 249, 249, 1),
                filled: true,
                contentPadding: EdgeInsets.only(left: 12),
                enabledBorder: OutlineInputBorder(
                    borderSide: BorderSide(color: rgba(249, 249, 249, 1)),
                    borderRadius: BorderRadius.all(Radius.circular(3))),
                hintText: '${commentProvider.commentPlaceHolderText}',
                hintStyle: TextStyle(color: Colors.grey, fontSize: 14),
                focusedBorder: OutlineInputBorder(
                    borderSide: BorderSide(color: rgba(249, 249, 249, 1)),
                    borderRadius: BorderRadius.all(Radius.circular(3))),
              ),
            ),
          ),
          Padding(
            padding: EdgeInsets.only(left: 30),
            child: InkWell(
              onTap: () {
                _postComment('v');
              },
              child: Image.asset('lib/assets/images/footer_send.png',
                  width: 22, height: 22),
            ),
          )
        ],
      ),
    );
  }

  //输入框获取焦点
  void getFocus(v) {
    FocusScope.of(context).requestFocus(_focusNode);
  }

  //输入框失去焦点
  void loseFocus() {
    FocusScope.of(context).unfocus();
  }

  //清空输入框
  void clearTextFild() {
    _textController.clear();
  }

  //发送评论
  void _postComment(v) {
    CommentModelProvider _formData = Provider.of<CommentModelProvider>(context);
    var content = _textController.text;
    if (content == '') {
      G.toast('请输入评论内容');
      widget.cancelInputView();
      return;
    }
    //发表评论
    if (_formData.isReplay == false) {
      print('进来了${widget.type}');
      print('${widget.id}');
      postComment(content, widget.type, widget.id.toString());
    }
    //回复评论
    if (_formData.isReplay == true) {
      replayComment(content, widget.type, widget.id.toString());
    }
  }

  //提交评论
  void postComment(String content, int type, String commentId) async {
    print('提交评论进来了');
    var result = await G.req.comment
        .addComment(content: content, commentType: type, objId: commentId);
    print('评论结果$result');
    if (result.data['status'] == 1) {
      G.toast(result.data['message']);
      CommentModelProvider _formData =
          Provider.of<CommentModelProvider>(context);
      _formData.loadUserComments(widget.id.toString(), widget.type);
      clearTextFild();
      widget.cancelInputView();
    }
  }

  //回复评论
  void replayComment(String content, int type, String newsId) async {
    CommentModelProvider _formData = Provider.of<CommentModelProvider>(context);
    var result = await G.req.comment.replayComment(
        content: content,
        commentType: type,
        commentId: _formData.currentCommentId,
        objId: newsId);
    if (result.data['status'] == 1) {
      G.toast(result.data['message']);
      CommentModelProvider _formData =
          Provider.of<CommentModelProvider>(context);
      _formData.loadUserComments(widget.id.toString(), widget.type);
      widget.cancelInputView();
    }
  }
}
